<!--
  子商户审核对话框组件
  @description 子商户审核功能的对话框组件
  @author AI Assistant
  @since 1.0.0
-->
<template>
  <Dialog v-model="dialogVisible" :title="t('settle.subMerchant.audit')" width="700px">
    <div v-loading="loading">
      <!-- 子商户信息展示 -->
      <el-descriptions :column="2" border class="mb-20px">
        <el-descriptions-item :label="t('settle.subMerchant.subMerchantNo')">
          {{ subMerchantData?.subMerchantNo }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.subMerchant.subMerchantName')">
          {{ subMerchantData?.subMerchantName }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.subMerchant.parentMerchantNo')">
          {{ subMerchantData?.parentMerchantNo }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.subMerchant.parentMerchantName')">
          {{ subMerchantData?.parentMerchantName }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.subMerchant.businessType')">
          <el-tag :type="getBusinessTypeTagType(subMerchantData?.businessType)">
            {{ businessTypeOptions[subMerchantData?.businessType || 0] }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.subMerchant.businessLicense')">
          {{ subMerchantData?.businessLicense }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.subMerchant.contactName')">
          {{ subMerchantData?.contactName }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.subMerchant.contactPhone')">
          {{ subMerchantData?.contactPhone }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.subMerchant.contactEmail')">
          {{ subMerchantData?.contactEmail || '-' }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.subMerchant.settleCycle')">
          {{ settleCycleOptions[subMerchantData?.settleCycle || 0] }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.subMerchant.feeRate')">
          {{ subMerchantData?.feeRate }}%
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.subMerchant.settleAccount')">
          {{ subMerchantData?.settleAccount }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.subMerchant.businessAddress')" :span="2">
          {{ subMerchantData?.businessAddress }}
        </el-descriptions-item>
        <el-descriptions-item :label="t('settle.common.createTime')" :span="2">
          {{ formatDate(subMerchantData?.createTime) }}
        </el-descriptions-item>
      </el-descriptions>
      
      <!-- 审核表单 -->
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px">
        <el-form-item :label="t('settle.subMerchant.auditResult')" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio :label="SubMerchantStatusEnum.APPROVED">
              <el-tag type="success">{{ t('settle.subMerchant.auditPass') }}</el-tag>
            </el-radio>
            <el-radio :label="SubMerchantStatusEnum.REJECTED">
              <el-tag type="danger">{{ t('settle.subMerchant.auditReject') }}</el-tag>
            </el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item :label="t('settle.subMerchant.auditRemark')" prop="remark">
          <el-input
            v-model="formData.remark"
            type="textarea"
            :placeholder="t('settle.common.pleaseInput') + t('settle.subMerchant.auditRemark')"
            :rows="4"
            maxlength="500"
            show-word-limit
          />
        </el-form-item>
      </el-form>
    </div>
    
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="loading">
        {{ t('settle.common.confirm') }}
      </el-button>
      <el-button @click="dialogVisible = false">
        {{ t('settle.common.cancel') }}
      </el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { useI18n } from 'vue-i18n'
import { formatDate } from '@/utils/formatTime'
import { auditSubMerchantApi } from '@/api/settle/subMerchant'
import type { SubMerchantVO } from '@/api/settle/types'
import { 
  SubMerchantStatusEnum, 
  BusinessTypeEnum, 
  SettleCycleEnum 
} from '@/api/settle/types'
import { Dialog } from '@/components/Dialog'

defineOptions({ name: 'SubMerchantAuditDialog' })

const { t } = useI18n()

// 弹窗相关
const dialogVisible = ref(false)
const loading = ref(false)

// 子商户数据
const subMerchantData = ref<SubMerchantVO>()

// 表单相关
const formRef = ref<FormInstance>()
const formData = ref({
  id: 0,
  status: SubMerchantStatusEnum.APPROVED,
  remark: ''
})

// 选项数据
const businessTypeOptions = {
  [BusinessTypeEnum.ENTERPRISE]: t('settle.subMerchant.businessTypes.enterprise'),
  [BusinessTypeEnum.INDIVIDUAL]: t('settle.subMerchant.businessTypes.individual'),
  [BusinessTypeEnum.MICRO]: t('settle.subMerchant.businessTypes.micro')
}

const settleCycleOptions = {
  [SettleCycleEnum.DAILY]: t('settle.subMerchant.settleCycles.daily'),
  [SettleCycleEnum.WEEKLY]: t('settle.subMerchant.settleCycles.weekly'),
  [SettleCycleEnum.MONTHLY]: t('settle.subMerchant.settleCycles.monthly')
}

// 表单校验规则
const formRules = reactive<FormRules>({
  status: [
    {
      required: true,
      message: t('settle.common.pleaseSelect') + t('settle.subMerchant.auditResult'),
      trigger: 'change'
    }
  ],
  remark: [
    {
      required: true,
      message: t('settle.common.pleaseInput') + t('settle.subMerchant.auditRemark'),
      trigger: 'blur'
    }
  ]
})

// 事件定义
const emit = defineEmits(['success'])

/**
 * 打开对话框
 */
const open = (subMerchant: SubMerchantVO) => {
  dialogVisible.value = true
  subMerchantData.value = subMerchant
  resetForm()
  formData.value.id = subMerchant.id
}

/**
 * 提交审核
 */
const submitForm = async () => {
  const valid = await formRef.value?.validate()
  if (!valid) return
  
  loading.value = true
  try {
    await auditSubMerchantApi(formData.value.id, formData.value.status, formData.value.remark)
    ElMessage.success(t('settle.subMerchant.tips.auditSuccess'))
    dialogVisible.value = false
    emit('success')
  } catch (error) {
    console.error('审核子商户失败:', error)
  } finally {
    loading.value = false
  }
}

/**
 * 重置表单
 */
const resetForm = () => {
  formData.value = {
    id: 0,
    status: SubMerchantStatusEnum.APPROVED,
    remark: ''
  }
  formRef.value?.resetFields()
}

// 标签类型获取函数
const getBusinessTypeTagType = (type?: number) => {
  if (!type) return ''
  const typeMap = {
    [BusinessTypeEnum.ENTERPRISE]: 'primary',
    [BusinessTypeEnum.INDIVIDUAL]: 'success',
    [BusinessTypeEnum.MICRO]: 'warning'
  }
  return typeMap[type] || ''
}

// 暴露方法
defineExpose({ open })
</script>

<style scoped>
.mb-20px {
  margin-bottom: 20px;
}
</style>
